/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.opgea.token.domain.model.JsonModelMap;
import com.opgea.token.domain.model.TokenStatus;
import com.opgea.token.domain.model.UserSessionData;
import com.opgea.token.service.ApplicationService;
import com.opgea.token.service.TokenDetailsService;
import com.opgea.token.service.TokenService;
import com.opgea.token.util.FacesUtil;
import com.opgea.token.web.dto.TokenDetailsDTO;

/**
 * 
 * @author Ramesh
 */
@Component
@Scope("request")
@ManagedBean(name = "waitingController")
@RequestScoped
public class TokenController {

    @Autowired
    private ApplicationService applicationService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private TokenDetailsService tokenDetailsService;

    private List<TokenStatus> waitingTokenList = new ArrayList<TokenStatus>();
    private List<TokenStatus> destinationTokenList = new ArrayList<TokenStatus>();
    
    public List<TokenStatus> getWaitingTokenList() {
	return waitingTokenList;
    }

    public void setWaitingTokenList(List<TokenStatus> waitingTokenList) {
	this.waitingTokenList = waitingTokenList;
    }

    public List<TokenStatus> getDestinationTokenList() {
	return destinationTokenList;
    }

    public void setDestinationTokenList(List<TokenStatus> destinationTokenList) {
	this.destinationTokenList = destinationTokenList;
    }

    public void waitingTokenList() {
	Long companyId = FacesUtil.getSessionUserDataObject().getCompanyId();
	waitingTokenList = applicationService.findAllTokenStatus(companyId);
    }

    public void destinationTokenList() {
	UserSessionData sessionData = FacesUtil.getSessionUserDataObject();
	Long companyId = sessionData.getCompanyId();
	Long destinationId = sessionData.getDestinationId();
	destinationTokenList = applicationService
		.findAllTokenStatusByDestination(companyId, destinationId);
    }

    @RequestMapping(value = "updateToken", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateToken(
	    @RequestParam(required = true, value = "tokenId") Long tokenId,
	    @RequestParam(required = true, value = "tokenStatus") Integer tokenStatus,
	    HttpServletRequest request) {

	TokenDetailsDTO tokenDetailsDTO = tokenDetailsService
		.findByTokenId(tokenId);
	tokenDetailsDTO.setTokenStatus(tokenStatus);
	System.out.println(tokenDetailsDTO);
	tokenDetailsService.update(tokenDetailsDTO);

	return JsonModelMap.success().data(tokenStatus);
    }

    /**
     * This is a method to send the response to the customer about the status of
     * a Token in a Queue based on the Organization.
     * 
     */
    /*
     * @RequestMapping(value = "tokenStatusInQueue", method = RequestMethod.GET)
     * public @ResponseBody Map<String, Object> tokenStatusInQueue(
     * 
     * @RequestParam(required = true, value = "orgId") Long organizationId,
     * 
     * @RequestParam(required = true, value = "tokenNo") Integer tokenNo,
     * HttpServletRequest request) {
     * 
     * TokenStatusReplyModel statusModel = tokenService.tokenReply(
     * organizationId, tokenNo);
     * 
     * return JsonModelMap.success().data(statusModel); }
     */
}
